
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property
    step: number = 10
    @property(cc.Node)
    player = null;
    bottom: number = 0;
    top: number = 0;
    left: number = 0;
    right: number = 0;
    // onLoad () {}

    start() {
        this.top = cc.view.getVisibleSize().height / 2 - this.player.height / 2;
        this.bottom = -cc.view.getVisibleSize().height / 2 + this.player.height / 2;
        this.left = -cc.view.getVisibleSize().width / 2 + this.player.width / 2;
        this.right = cc.view.getVisibleSize().width / 2 - this.player.width / 2;
        let self = this;
        
        //绑定键盘按下事件
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function (event: cc.Event.EventKeyboard) {
            switch (event.keyCode) {
                case cc.macro.KEY.down:
                    self.player.y - self.step < self.bottom ? self.player.y = self.bottom : self.player.y -= self.step;
                    break;
                case cc.macro.KEY.up:
                    self.player.y + self.step > self.top ? self.player.y = self.top : self.player.y += self.step;
                    break;
                case cc.macro.KEY.left:
                    self.player.x - self.step < self.left ? self.player.x = self.left : self.player.x -= self.step;
                    break;
                case cc.macro.KEY.right:
                    self.player.x + self.step > self.right ? self.player.x = self.right : self.player.x += self.step;
                    break;
            }
        }, this);
    }

    // update (dt) {}
}
